var waybill_storelist = {
    init: function() {
        var that = this;
        $.ajax({
            url: COMMONURL + "/api/bidload/GetLoadList?token=" + $.cookie('token'),
            type: "post",
            data: {
                page: 1,
                pagesize: 10
            },
            dataType: "json"
        }).done(function(redata) {
            if (redata.recode == '0000') {
                var compiled = _.template($("#waybillStoreListTpl").html());
                var odata = redata.mod.Items;
                // alert(that.format(odata[0].start_bid_time))
                for (var i = 0, length = odata.length; i < length; i++) {
                    odata[i].start_bid_time = that.format(odata[i].start_bid_time);
                    odata[i].end_bid_time = that.format(odata[i].end_bid_time);
                    odata[i].consignee_time = that.format(odata[i].consignee_time);
                    odata[i].load_time=(odata[i].load_time!=null && odata[i].load_time!='')?odata[i].load_time.replace('T',' ').replace('Z',''):'';
                }
                $("#waybillStoreListCon").html(compiled(odata));

                $("#waybillStoreListdemo").jqPaginator({
                    totalPages: Math.ceil(redata.mod.TotalNum / 20),
                    visiblePages: 5,
                    currentPage: 1,
                    first: '<li class="first"><a href="javascript:void(0);">首页<\/a><\/li>',
                    prev: '<li class="prev"><a href="javascript:void(0);"><i class="arrow arrow2"><\/i>上一页<\/a><\/li>',
                    next: '<li class="next"><a href="javascript:void(0);">下一页<i class="arrow arrow3"><\/i><\/a><\/li>',
                    last: '<li class="last"><a href="javascript:void(0);">末页<\/a><\/li>',
                    page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
                    onPageChange: function (num, type) {
                        $("#warestorelistpage").val(num);
                        if (type == 'change') {
                            that.doPage();
                        }
                    }
                });


                //备注
                $('#remarkModel').on('show.bs.modal', function(event) {
                    var button = $(event.relatedTarget),
                        id = button.data('id');

                    $(this).attr({
                        'aid': id
                    });
                });
                //备注提交
                $("#remarkSubmit").click(function() {
                    var id = $('#remarkModel').attr('aid'),
                        remark = $.trim($("#message-text").val());
                    $.ajax({
                        url: COMMONURL + "api/BidLoad/UpdateRemark?token=" + $.cookie('token'),
                        type: "post",
                        data: {
                            id: id,
                            load_remark: remark
                        }
                    }).done(function(redata) {
                        if (redata.recode == '0000') {
                            alert('备注修改成功');
                            window.location.reload();
                        }
                    });

                });

                //已取货
                $('.hasnopickModel').on('click', function(event) {
                    var load_no = $(this).attr("data-loadno");
                    var bid_id = $(this).attr("data-bid");
                    $.ajax({
                        url: COMMONURL + "api/BidLoad/BillPick?token=" + $.cookie('token'),
                        type: "get",
                        data: {
                            load_no: load_no,
                            bid_id:bid_id
                        },
                        dataType: "json"
                    }).done(function(redata) {
                        if (redata.recode == '0000') {
                            alert("取货操作成功！")
                            window.location.reload();

                        }else{
                            alert("取货操作失败！")
                        }

                    });
                });

            }
        });
    },
    doPage: function () {
        var that = this;
        var page = $.trim($("#warestorelistpage").val());
        //alert(page);
        $.ajax({
            url: COMMONURL + "/api/bidload/GetLoadList?token=" + $.cookie('token'),
            type: "post",
            data: {
                "page": page,
                "pagesize": 10
            },
            dataType: "json"
        }).done(function (redata) {
            if (redata.recode == '0000' && redata.mod.Items.length > 0) {
                var compiled = _.template($("#waybillStoreListTpl").html());
                var odata = redata.mod.Items;
                //alert(odata[0].load_time);
                for (var i = 0, length = odata.length; i < length; i++) {
                    odata[i].start_bid_time = that.format(odata[i].start_bid_time);
                    odata[i].end_bid_time = that.format(odata[i].end_bid_time);
                    odata[i].load_time = (odata[i].load_time != null && odata[i].load_time != '') ? odata[i].load_time.replace('T', ' ').replace('Z', '') : '';
                }
                // alert(odata[0].load_time);
                $("#waybillStoreListCon").html(compiled(odata));

                //已取货
                $('.hasnopickModel').on('click', function (event) {
                    var load_no = $(this).attr("data-loadno");
                    var bid_id = $(this).attr("data-bid");
                    //alert(bid_id);
                    $.ajax({
                        url: COMMONURL + "api/BidLoad/BillPick?token=" + $.cookie('token'),
                        type: "get",
                        data: {
                            load_no: load_no,
                            bid_id: bid_id
                        },
                        dataType: "json"
                    }).done(function (redata) {
                        if (redata.recode == '0000') {
                            alert("取货操作成功！")
                            window.location.reload();
                        } else {
                            alert("取货操作失败！")
                        }

                    });
                });
               

            } else {
                var compiled = _.template($("#waybillStoreListTpl").html());
                var odata = redata.mod.Items;
                $("#waybillStoreListCon").html(compiled(redata.mod));
                $("#waybillStoreListdemo").hide();
            }
        });
    },
    format: function(nS) {
       // return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ');
             // return new Date(parseInt(nS) * 1000).toLocaleString().substr(0,17);
                if(!nS) return null;
            var date = new Date(nS*1000);
            var year = date.getFullYear();
            var month = date.getMonth()+1;
            var d = date.getDate();
            var hours = date.getHours();
            var minutes = date.getMinutes();
            var seconds = date.getSeconds();
            return year+'-'+month+'-'+d+' '+hours+':'+minutes;

    }
}
waybill_storelist.init();
